﻿using System;
using System.Reflection;
using System.Windows.Forms;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmDsThiTotNghiepVanHoa : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "thi_cu";
        private bool mblnIsValid = false;

        private int mintKVH;
        private int mintLopID;
        private int mintMonID;
        private int mintCapDoID;
        private string mstrMon;
        private string mstrCapDo;
        private string mstrLop;
        private string mstrKhoas;

        public frmDsThiTotNghiepVanHoa()
        {
            InitializeComponent();
        }

        private void frmQuanLyDiemVanHoa_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mKhoaHoc' table. You can move, or remove it, as needed.
            this.mKhoaHocTableAdapter.Fill(this.qLTHDataSet.mKhoaHoc);
            // TODO: This line of code loads data into the 'qLTHDataSet.mKhungVanHoaGetFull' table. You can move, or remove it, as needed.
            this.mLopVanHoaSelectAllWithDetailTableAdapter.Fill(this.qLTHDataSet.mLopVanHoaSelectAllWithDetail);
        }

        private void grdControlLop_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                //if (!validControlBlank.Validate()) return;

                mintKVH = clsCommon.fncCnvNullToInt(dgvLopVanHoa.GetFocusedRowCellValue(colKVH_ID));

                if (mintKVH <= 0) return;

                Cursor.Current = Cursors.WaitCursor;

                mintLopID = clsCommon.fncCnvNullToInt(dgvLopVanHoa.GetFocusedRowCellValue(colLVH_ID));
                //mintMonID = clsCommon.fncCnvNullToInt(dgvLopVanHoa.GetFocusedRowCellValue(colMVH_ID));
                //mstrMon = clsCommon.fncCnvNullToString(dgvLopVanHoa.GetFocusedRowCellValue(colMVH_ten));
                mstrLop = clsCommon.fncCnvNullToString(dgvLopVanHoa.GetFocusedRowCellValue(colLVH_ten));
                mstrKhoas = clsCommon.fncCnvNullToString(dgvLopVanHoa.GetFocusedRowCellDisplayText(colKHS_ID));

                grpMonHoc.Text = string.Format("Điểm thi tốt nghiệp khóa {0} lớp {1}", mstrKhoas, mstrLop);

                // TODO: This line of code loads data into the 'qLTHDataSet.tHocSinhMon_GetFull' table. You can move, or remove it, as needed.
                this.usrProc_GetDanhSachThiTotNghiepVanHoaTableAdapter.Fill(this.qLTHDataSet.usrProc_GetDanhSachThiTotNghiepVanHoa, mintLopID);
                dgvDiem.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void mniSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                dgvDiem.UpdateCurrentRow();
                if (!mblnIsValid) return;

                Cursor.Current = Cursors.WaitCursor;

                DTO.THocSinhDiemTotNghiepVanHoaDTO objHsDiemToan;
                DTO.THocSinhDiemTotNghiepVanHoaDTO objHsDiemLy;
                DTO.THocSinhDiemTotNghiepVanHoaDTO objHsDiemHoa;

                bool blnSuccess = true;
                int intHsID;

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    for (int i = 0; i <= dgvDiem.RowCount - 1; i++)
                    {
                        if (!blnSuccess) break;

                        intHsID = clsCommon.fncCnvNullToInt(dgvDiem.GetRowCellValue(i, colHS_ID));

                        objHsDiemToan = DAO.THocSinhDiemTotNghiepVanHoaDAO.Select(Program.gobjDb, intHsID, (int)clsEnum.MON_VAN_HOA.TOAN);
                        objHsDiemLy = DAO.THocSinhDiemTotNghiepVanHoaDAO.Select(Program.gobjDb, intHsID, (int)clsEnum.MON_VAN_HOA.LY);
                        objHsDiemHoa = DAO.THocSinhDiemTotNghiepVanHoaDAO.Select(Program.gobjDb, intHsID, (int)clsEnum.MON_VAN_HOA.HOA);

                        objHsDiemToan.Tnvh_diemtotnghiep = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colTotNghiepToan));
                        objHsDiemLy.Tnvh_diemtotnghiep = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colTotNghiepLy));
                        objHsDiemHoa.Tnvh_diemtotnghiep = clsCommon.fncCnvNullToFloat(dgvDiem.GetRowCellValue(i, colTotNghiepHoa));

                        blnSuccess = blnSuccess && DAO.THocSinhDiemTotNghiepVanHoaDAO.Update(Program.gobjDb, Program.gobjTrans, objHsDiemToan);
                        blnSuccess = blnSuccess && DAO.THocSinhDiemTotNghiepVanHoaDAO.Update(Program.gobjDb, Program.gobjTrans, objHsDiemLy);
                        blnSuccess = blnSuccess && DAO.THocSinhDiemTotNghiepVanHoaDAO.Update(Program.gobjDb, Program.gobjTrans, objHsDiemHoa);
                    }

                    //commit dữ liệu nếu không có gì lỗi, ngược lại thì rollback
                    if (blnSuccess) { Program.gobjTrans.Commit(); }
                    else
                    { Program.gobjTrans.Rollback(); }
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess) { clsCommon.fncMsgInfo("Cập nhật thành công."); }
                else { clsCommon.fncMsgError("Cập nhật thất bại. Xin hãy thử lại."); }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void dgvDiem_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                string strColName = "";
                float fltDiem = 0.0F;
                mblnIsValid = true;

                strColName = colTotNghiepToan.FieldName;
                if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }
                if (fltDiem < 0 || fltDiem > 10)
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }

                strColName = colTotNghiepLy.FieldName;
                if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }
                if (fltDiem < 0 || fltDiem > 10)
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }

                strColName = colTotNghiepHoa.FieldName;
                if (!float.TryParse(dgvDiem.GetRowCellValue(e.RowHandle, strColName).ToString(), out fltDiem))
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }
                if (fltDiem < 0 || fltDiem > 10)
                {
                    e.Valid = false;
                    dgvDiem.SetColumnError(dgvDiem.Columns[strColName], "Điểm không hợp lệ.");
                    mblnIsValid = false;
                    return;
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvDiem_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            try
            {
                e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnXuatKetQua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (mintKVH <= 0) return;

                Cursor.Current = Cursors.WaitCursor;

                Export.frmExportExcel objForm = new Export.frmExportExcel();
                if (!objForm.fncExportDanhSachThiTotNghiepVanHoa_KetQua(this.qLTHDataSet.usrProc_GetDanhSachThiTotNghiepVanHoa)) return;
                objForm.MdiParent = this.MdiParent;
                objForm.Show();

                //List<DTO.TLopVanHoaHocSinhDTO> lstHs = DAO.TLopVanHoaHocSinhDAO.SelectAll(Program.mobjDb);

                //DTO.THocSinhDiemTotNghiepVanHoaDTO objHsTotNghiep = new DTO.THocSinhDiemTotNghiepVanHoaDTO();

                //bool blnSuccess = true;
                //Program.mobjTrans = Program.mobjConn.BeginTransaction();

                //for (int i = 0; i <= lstHs.Count - 1; i++)
                //{
                //    if (!blnSuccess) break;

                //    objHsTotNghiep.HS_ID = lstHs[i].HS_ID;
                //    objHsTotNghiep.Tnvh_diemtotnghiep = 0.0F;

                //    objHsTotNghiep.MVH_ID = (int)clsEnum.MON_VAN_HOA.TOAN;
                //    blnSuccess = blnSuccess && DAO.THocSinhDiemTotNghiepVanHoaDAO.Insert(Program.mobjDb, Program.mobjTrans, objHsTotNghiep);

                //    objHsTotNghiep.MVH_ID = (int)clsEnum.MON_VAN_HOA.LY;
                //    blnSuccess = blnSuccess && DAO.THocSinhDiemTotNghiepVanHoaDAO.Insert(Program.mobjDb, Program.mobjTrans, objHsTotNghiep);

                //    objHsTotNghiep.MVH_ID = (int)clsEnum.MON_VAN_HOA.HOA;
                //    blnSuccess = blnSuccess && DAO.THocSinhDiemTotNghiepVanHoaDAO.Insert(Program.mobjDb, Program.mobjTrans, objHsTotNghiep);

                //}

                //if (blnSuccess)
                //{
                //    Program.mobjTrans.Commit();
                //    MessageBox.Show("thanh cong");
                //}
                //else
                //{
                //    Program.mobjTrans.Rollback();
                //    MessageBox.Show("that bai");
                //}
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
    }
}